package com.wypesoft.android;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class testOpenCVActivity extends Activity implements OnClickListener{
	private static final String IMAGE_DIRECTORY = "/sdcard/DCIM/Camera";
	private static final int ACTIVITY_SELECT_CAMERA = 0;
	private static final int ACTIVITY_SELECT_IMAGE = 1;
	private static final String TAG = "MAIN_ACTIVITY";
	private static final int CAMERA_ID = Menu.FIRST;
	private static final int GALLERY_ID = Menu.FIRST + 1;
	private String mCurrentImagePath = null;
	private OpenCV opencv = new OpenCV();
	private ImageView mImageView;
	private byte[] imageData;
	private Bitmap bitmap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageView = new ImageView(this);
		setContentView(R.layout.main);
		
		Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, CAMERA_ID, 0, "Obtener Foto");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case CAMERA_ID:
			Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			long timeTaken = System.currentTimeMillis();
			mCurrentImagePath = IMAGE_DIRECTORY + "/"
					+ Utility.createName(timeTaken) + ".jpg";
			Log.i(TAG, mCurrentImagePath);
			cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(mCurrentImagePath)));
			startActivityForResult(cameraIntent, ACTIVITY_SELECT_CAMERA);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ACTIVITY_SELECT_CAMERA
				&& resultCode == Activity.RESULT_OK) {
			
			ContentValues values = new ContentValues();
			int degrees = Utility.getRotationFromImage(mCurrentImagePath);
			try {
				ExifInterface exif = new ExifInterface(mCurrentImagePath);
				float[] position = new float[2];
				if (exif.getLatLong(position)) {
					values.put(Images.Media.LATITUDE, position[0]);
					values.put(Images.Media.LONGITUDE, position[1]);
				}
			} catch (Exception e) {

			}
			// reduce the size of image
			try {
				BitmapFactory.Options option = new BitmapFactory.Options();
				option.inSampleSize = 4;
				bitmap = BitmapFactory.decodeFile(mCurrentImagePath,
						option);

				int width = bitmap.getWidth();
				int height = bitmap.getHeight();
				int[] pixels = new int[width * height];
				bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
				opencv.setSourceImage(pixels, width, height);
				
				setContentView(mImageView);
				
				
				opencv.haar();
				        				
				imageData = opencv.getSourceImage();
				        				
				bitmap = BitmapFactory.decodeByteArray(imageData, 0,imageData.length);
				        				
				        				
				mImageView.setImageBitmap(bitmap);
				        			
				
				
				
				FileOutputStream out = new FileOutputStream(mCurrentImagePath);
				bitmap.compress(CompressFormat.JPEG, 100, out);
				
				
			} catch (Exception e) {

			}
			values.put(Images.Media.MIME_TYPE, "image/jpeg");
			values.put(Images.Media.DATA, mCurrentImagePath);
			values.put(Images.Media.ORIENTATION, degrees);
			getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
			
			
		}
	
		
	}

	@Override
	public void onClick(View v) {

		Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		long timeTaken = System.currentTimeMillis();
		mCurrentImagePath = IMAGE_DIRECTORY + "/"
				+ Utility.createName(timeTaken) + ".jpg";
		Log.i(TAG, mCurrentImagePath);
		cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(new File(mCurrentImagePath)));
		
		startActivityForResult(cameraIntent, ACTIVITY_SELECT_CAMERA);
		       
		
		
	}
	
	
}